import { MaterialModel } from '~/model'
import { OrderItemModel } from '~/model/OrderItemModel'

export abstract class OrderItemSerializer {
  static serialize(model: OrderItemModel): SentData {
    return {
      materialId: model.material!.id,
      quantity: model.quantity,
      unitPrice: model.unitPrice,
      amount: model.amount,
    }
  }

  static async deserialize(data: ReceivedData): Promise<OrderItemModel> {
    const model = new OrderItemModel()
    model.material = await MaterialModel.fromId(data.materialId)
    model.quantity = data.quantity
    model.unitPrice = data.unitPrice
    model.amount = data.amount
    return model
  }
}

interface SentData {
  materialId: number
  quantity: number
  unitPrice: number
  amount: number
}

interface ReceivedData {
  materialId: number
  quantity: number
  unitPrice: number
  amount: number
}
